package com.shop.shoppingmall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.dao.ShVipDao;
import com.shop.shoppingmall.entity.ShBackUser;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.entity.ShVip;
import com.shop.shoppingmall.service.ShVipService;
import com.shop.shoppingmall.service.ShWalletService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.DateUtils;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * vip状态(ShVip)表服务实现类
 *
 * @author makejava
 * @since 2020-09-05 14:22:41
 */
@Service("shVipService")
@Transactional
@Slf4j
public class ShVipServiceImpl implements ShVipService {
    @Resource
    private ShVipDao shVipDao;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Resource
    private ShUserDao shUserDao;
    @Resource
    private ShWalletService shWalletService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShVip queryById(String id) {
        return this.shVipDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ShVip> queryAllByLimit(int offset, int limit) {
        return this.shVipDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shVip 实例对象
     * @return 实例对象
     */
    @Override
    public ShVip insert(ShVip shVip) {
        this.shVipDao.insert(shVip);
        return shVip;
    }

    /**
     * 修改数据
     *
     * @param shVip 实例对象
     * @return 实例对象
     */
    @Override
    public ShVip update(ShVip shVip) {
        this.shVipDao.update(shVip);
        return this.queryById(shVip.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.shVipDao.deleteById(id) > 0;
    }


    /**
     * 根据用户id查询该用户是否充值会员
     * @param userId
     * @return
     */
    @Override
    public Result queryByUserId(String userId) {
        Result<JSONObject> result = new Result<JSONObject>();
        ShVip shVip = shVipDao.queryByUserId(userId);
        JSONObject obj = new JSONObject();
        if(shVip != null){
            if(DateUtils.isEffectiveDate(new Date(),shVip.getStartTime(),shVip.getEndTime())){
                obj.put("status","1");
            }else{
                obj.put("status","0");
            }
            obj.put("shVip", shVip);
            result.setCode(101);
            result.setResult(obj);
            result.setSuccess(true);
            return result;
        }else{
            result.setCode(102);;
            result.setSuccess(false);
            return result;
        }

    }


    @Override
    public void updateByUserId(String userId) {
        log.info("会员开通信息执行<><><><><><><><><><><><>");
        ShVip shVip = shVipDao.queryByUserId(userId);
        if(shVip != null){
            String dd = DateUtils.addDateMinut(shVip.getEndTime(),1);
            System.out.println(dd);
            Date date = DateUtils.str2Date(dd,new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"));
            System.out.println(date+"======-==");
            shVip.setEndTime(date);
            shVipDao.update(shVip);
            shWalletService.updateSpecialCoin(userId,new BigDecimal(168));
            log.info("续费会员用户id为"+userId);
        }else{
            ShVip shVip1 = new ShVip();
            shVip1.setId(UUIDGenerator.generate());
            shVip1.setUserId(userId);
            shVip1.setStartTime(new Date());
            String dd = DateUtils.addDateMinut(shVip1.getStartTime(),1);
            System.out.println(dd);
            Date date = DateUtils.str2Date(dd,new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"));
            System.out.println(date+"======-==");
            shVip1.setEndTime(date);
            shVipDao.insert(shVip1);
            shWalletService.updateSpecialCoin(userId,new BigDecimal(168));
            log.info("新入驻会员id为"+userId+"会员已开通");
        }


    }

    /**
     * 开通会员
     * @param request
     * @return
     */
   /* @Override
    public Result insertVip(HttpServletRequest request) {
        String nameByToken = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(nameByToken);
        if(shUser == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        String userId = shUser.getId();
        ShVip shVip = shVipDao.queryByUserId(userId);
        if(shVip != null){
            return Result.error("您已开通会员！");
        }
        shVip.setId(UUIDGenerator.generate());
        shVip.setUserId(userId);
        shVip.setStartTime(new Date());
        shVip.setEndTime(new Date());
        shVipDao.insert(shVip);
        return null;
    }*/
}
